package org.xdoclet.plugin.command;

import java.util.Collection;
import java.util.*;

import org.generama.JellyTemplateEngine;
import org.generama.QDoxCapableMetadataProvider;
import org.generama.WriterMapper;
import org.generama.defaults.QDoxPlugin;

import com.thoughtworks.qdox.model.JavaClass;
import com.thoughtworks.qdox.model.DocletTag;

import org.xdoclet.plugin.command.qtags.TagLibrary;

public class CommandPlugin extends QDoxPlugin {

  public CommandPlugin(JellyTemplateEngine jellyTemplateEngine,
         QDoxCapableMetadataProvider metadataProvider, WriterMapper writerMapper) 
                      throws ClassNotFoundException {

    super (jellyTemplateEngine, metadataProvider, writerMapper);
    setFilereplace ("command-mapping.xml");
    setMultioutput (false);
        
    /*The following line of code is a programmatic way of registering the tags*/

    /*metadataProvider.getDocletTagFactory ().registerTag ("command.class",
                      org.xdoclet.plugin.command.qtags.CommandClassTagImpl.class);*/

    /* xdoclet2 recommended way of registering custom tags*/

    new TagLibrary(metadataProvider);
  }

  /**
   * A helper method on the Plugin class that fetches the name of the command. 		
   *  
   */

  public String getCommandName(Object metadata){
    JavaClass javaClass = (JavaClass) metadata;
    return javaClass.getNamedParameter("command.class","name");
  }

  // Checks if the source file is a Command interface and that you do 
  // want to consider it for code generation.

  public boolean shouldGenerate(Object metadata) {
    JavaClass javaClass = (JavaClass) metadata;
    boolean isCommand = javaClass.isA("org.xdoclet.plugin.commands.Command");
    boolean ignore = "false".equals(javaClass.getNamedParameter("command.class","generate"));
    if (isCommand && !ignore)
      return true;
    else
      return false;
  }
}